using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace DataEntity
{
	public class T_Project_Referral : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlInt32		_project_Referral_Type;
            private SqlDecimal      _project_Referral_Tally;
			private SqlGuid			_project_Referral_ID, _project_Referral_MainID;
		#endregion








		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Project_Referral_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Referral_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Referral_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Referral_MainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Referral_MainID));
				cmdToExecute.Parameters.Add(new SqlParameter("@iProject_Referral_Type", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _project_Referral_Type));
                cmdToExecute.Parameters.Add(new SqlParameter("@sProject_Referral_Tally", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 1, "", DataRowVersion.Proposed, _project_Referral_Tally));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Project_Referral::Update::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}





		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Project_Referral_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("t_Project_Referral");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Referral_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Referral_ID));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_project_Referral_ID = (Guid)toReturn.Rows[0]["Project_Referral_ID"];
					_project_Referral_MainID = (Guid)toReturn.Rows[0]["Project_Referral_MainID"];
					_project_Referral_Type = toReturn.Rows[0]["Project_Referral_Type"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["Project_Referral_Type"];
                    _project_Referral_Tally = toReturn.Rows[0]["Project_Referral_Tally"] == System.DBNull.Value ? SqlDecimal.Null : (Decimal)toReturn.Rows[0]["Project_Referral_Tally"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Project_Referral::SelectOne::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}





		#region Class Property Declarations
		public SqlGuid Project_Referral_ID
		{
			get
			{
				return _project_Referral_ID;
			}
			set
			{
				_project_Referral_ID = value;
			}
		}


		public SqlGuid Project_Referral_MainID
		{
			get
			{
				return _project_Referral_MainID;
			}
			set
			{
				SqlGuid project_Referral_MainIDTmp = (SqlGuid)value;
				if(project_Referral_MainIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Project_Referral_MainID", "Project_Referral_MainID can't be NULL");
				}
				_project_Referral_MainID = value;
			}
		}


		public SqlInt32 Project_Referral_Type
		{
			get
			{
				return _project_Referral_Type;
			}
			set
			{
				SqlInt32 project_Referral_OutCommIDTmp = (SqlInt32)value;
				if(project_Referral_OutCommIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Project_Referral_Type", "Project_Referral_Type can't be NULL");
				}
				_project_Referral_Type = value;
			}
		}


        public SqlDecimal Project_Referral_Tally
		{
			get
			{
                return _project_Referral_Tally;
			}
			set
			{
                SqlDecimal project_Referral_OutCommTmp = (SqlDecimal)value;
				if(project_Referral_OutCommTmp.IsNull)
				{
                    throw new ArgumentOutOfRangeException("Project_Referral_Tally", "Project_Referral_Tally can't be NULL");
				}
                _project_Referral_Tally = value;
			}
		}
		#endregion
	}
}
